package cn.fatgod.learn.thymeleaf.binder;

import cn.fatgod.learn.thymeleaf.standard.enums.PermissionModifier;
import cn.fatgod.learn.thymeleaf.standard.interfaces.Bind;
import cn.fatgod.learn.thymeleaf.standard.interfaces.IDataBinder;
import cn.fatgod.learn.thymeleaf.standard.interfaces.Importable;
import cn.fatgod.learn.thymeleaf.standard.interfaces.Modifiable;
import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字段数据绑定器
 */
@Bind
public class FieldDataBinder implements IDataBinder, Modifiable, Importable {
    /**
     * 是否启用
     */
    @Bind(false)
    private boolean enable = true;

    /**
     * 权限修饰符
     */
    private PermissionModifier modifier = PermissionModifier.PRIVATE;

    /**
     * 字段名称
     */
    private String name;

    /**
     * 字段类型
     */
    private String type;

    /**
     * 导入路径列表
     */
    private String importPath;

    /**
     * 模型注解列表
     */
    private final List<AnnotationDataBinder> annotations = new ArrayList<>();

    @Override
    public boolean isEnable() {
        return enable;
    }

    @Override
    public PermissionModifier getModifier() {
        return modifier;
    }

    @Override
    public List<String> getImportPaths() {
        List<String> paths = annotations.stream().flatMap(prop -> prop.getImportPaths().stream())
                .distinct()
                .collect(Collectors.toList());
        if (StrUtil.isNotBlank(this.importPath)) {
            paths.add(this.importPath);
        }
        return paths;
    }

    public FieldDataBinder setEnable(boolean enable) {
        this.enable = enable;
        return this;
    }

    public FieldDataBinder setModifier(PermissionModifier modifier) {
        this.modifier = modifier;
        return this;
    }

    public String getName() {
        return name;
    }

    public FieldDataBinder setName(String name) {
        this.name = name;
        return this;
    }

    public String getType() {
        return type;
    }

    public FieldDataBinder setType(String type) {
        return this.setType(type, StrUtil.EMPTY);
    }

    public FieldDataBinder setType(String type, String importPath) {
        this.type = type;
        this.importPath = importPath;
        return this;
    }

    public FieldDataBinder addAnnotation(AnnotationDataBinder binder) {
        this.annotations.add(binder);
        return this;
    }

    public FieldDataBinder addAnnotations(List<AnnotationDataBinder> binders) {
        this.annotations.addAll(binders);
        return this;
    }

    public List<AnnotationDataBinder> getAnnotations() {
        return annotations;
    }
}
